package a.autocode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jplat.tools.string.StringUtil;
import jplat.tools.string.XLog;

/**
 * 字段域信息.
 * @author zhangcq
 * @date Dec 9, 2016
 * @comment
 */
public class JBaseFieldInfo
{
	/**
	 * 域类型.
	 */
	public static String TP_STRING = "S";
	public static String TP_INT = "N10";
	public static String TP_DOUBLE = "N10.2";
	public static String TP_LIST="LIST";

	public static String OK="OK";
	public static String REQUIED_YES="Y";		//必输项 是-必输 Y
	public static String REQUIED_NO="N";		//必输项 否-不是必输  N

	//类型Map.
	public static Map<String,String> typeMap = new HashMap<String,String>();
	
	//必输
	public static Map<String,String> requiredMap = new HashMap<String,String>();
	static
	{
		typeMap.put("S", OK);
		typeMap.put("N", OK);
		typeMap.put(TP_LIST, OK);
		
		requiredMap.put(REQUIED_NO, OK);
		requiredMap.put(REQUIED_YES, OK);
	}
	
	/**
	 * 域值.
	 */
	protected String name;			//字段名字.
	protected String type;			//类型.
	protected String length;		//长度.
	
	protected String chName;		//中文名字.
	protected String desc;			//描述.
	protected boolean required;		//是否必须输.
	
	/**
	 * 集合对象引用列.
	 */
	public List<JBaseFieldInfo> fieldList;		//类型为LIST时候的引用类名.
	
	public JBaseFieldInfo()
	{
		fieldList = new ArrayList<JBaseFieldInfo>();
	}
	
	public JBaseFieldInfo( String name__ )
	{
		this();
		name = name__;
	}
	
	/**
	 * 生成成员变量行
	 * @author zhangcq
	 * @date Dec 6, 2016
	 * @comment 
	 * @param transCode
	 * @param beanType
	 * @return
	 */
	public String javaFormat( String transCode,String beanType )
	{
		return "private "+cnv2JavaType(transCode,beanType)+" "+StringUtil.lowFirstCase(name);
	}
	
	/**
	 * 获取Field的javaType
	 * @author zhangcq
	 * @date Dec 6, 2016
	 * @comment 
	 * @param transCode
	 * @param beanType
	 * @return
	 */
	public String cnv2JavaType( String nameKey, String beanType )
	{

		if ( TP_STRING.equals(type) )
		{
			return "String";
		}
		else if ( TP_INT.equals(type) )
		{
			return "int";
		}
		else if ( TP_DOUBLE.equals(type) )
		{
			return "double";
		}
		else if ( TP_LIST.equals(type) )
		{
			return "List<"+javaListBeanTypeName( nameKey, beanType )+">";		
		}
		else
		{
			System.out.println("invalid type for field:"+type+" "+name);
		}
		
		return "";
	}
	
	/**
	 * 生成getter-setters的属性方法.
	 * @author zhangcq
	 * @date Dec 6, 2016
	 * @comment 
	 * @param transCode
	 * @param type
	 * @return
	 */
	public String javaProtertyString( String nameKey, String type )
	{
		StringBuffer sb = new StringBuffer();
		sb.append("\tpublic "+cnv2JavaType(nameKey,type)+" get"+StringUtil.bigFirstCase(name)+"( ) {\n\t\treturn "+StringUtil.lowFirstCase(name)+";\n\t}\n\n");
		sb.append("\tpublic void set"+StringUtil.bigFirstCase(name)+"("+cnv2JavaType(nameKey,type) +" arg0 ) {\n\t\t"+StringUtil.lowFirstCase(name)+" = arg0;\n\t}\n\n");
		
		return sb.toString();
	}
	
	/**
	 * bean的set方法.
	 * @author zhangcq
	 * @date Jan 5, 2017
	 * @comment 
	 * @param objName
	 * @param nameKey
	 * @param type
	 * @return
	 */
	public String javaCallSetString( String objName, String nameKey, String type )
	{
		StringBuffer sb = new StringBuffer();
		sb.append(objName+".set"+StringUtil.bigFirstCase(name)+"(null);").append("\t\t\t//").append(chName).append(" ").append(desc);
		
		return sb.toString();
	}
	
	/**
	 * 当一个域为一个list的时候，返回该域对应的类型.
	 * @author zhangcq
	 * @date Dec 9, 2016
	 * @comment 
	 * @param nameKey
	 * @param type
	 * @return
	 */
	public String javaListBeanTypeName( String nameKey, String type )
	{
		//PCM1008ReqCardListModel (list model)
		return StringUtil.bigFirstCase(nameKey)
				+StringUtil.bigFirstCase(type)
				+StringUtil.bigFirstCase(name)+"Model";
	}
	
	//解析类型.
	public String getDesc() {
		return desc;
	}
	public void setDesc(String __desc) {
		this.desc = __desc;
	}
	public String getChName() {
		return chName;
	}

	public void setChName(String chName) {
		this.chName = chName;
	}

	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	
	public String getLength() {
		return length;
	}

	public void setLength(String length) {
		this.length = length;
	}

	public boolean isRequired() {
		return required;
	}
	public void setRequired(boolean required) {
		this.required = required;
	}

	public List<JBaseFieldInfo> getFieldList() {
		return fieldList;
	}

	public void setFieldList(List<JBaseFieldInfo> fieldList) {
		this.fieldList = fieldList;
	}
	
	public static void main(String args[])
	{
		JBaseFieldInfo field = new JBaseFieldInfo();
		field.setDesc("名字第三方 你好:sldfjlsjdf sljfdklj-sdlfkjl");
		
		XLog.log("chName=%s,desc=%s", field.getChName(),field.getDesc());
	}
}
